package com.nf.health.app.inquiry.impl.viewholder;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hyphenate.easeui.widget.CircularImage;
import com.nf.health.app.R;
import com.nf.health.app.customview.MyFlowLayout;
import com.nf.health.app.models.DoctorCommnetVo;
import com.nf.health.app.utils.DateUtil;
import com.nf.health.app.utils.GetToast;
import com.nf.health.app.utils.ImageUtils;

import org.json.JSONArray;
import org.json.JSONException;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Jason on 2016/9/21.
 * 家庭服务，付费咨询，健康管理
 */
public class DoctorCommentViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    @Bind(R.id.iv_img_discuss)
    CircularImage iv_img_discuss;//医生的头像

    @Bind(R.id.tv_discuss_doc_name)//医生姓名
    TextView tv_discuss_doc_name;
    @Bind(R.id.tv_is_satisfied)//满意程度
    TextView tv_is_satisfied;
    @Bind(R.id.mygv_status_satify)//存放GidView的满意程度
        MyFlowLayout mFlowLayout;
    @Bind(R.id.tv_discuss_doc_time)//评论的时间
    TextView tv_discuss_doc_time;
    @Bind(R.id.tv_word_discuss)//医生评论
    TextView tv_word_discuss;

    private LayoutInflater layoutInflater;

    private Context cnt;
    public DoctorCommentViewHolder(View itemView, Context cnt,LayoutInflater layoutInflater) {
        super(itemView);
        ButterKnife.bind(this, itemView);
        this.cnt = cnt;
        this.layoutInflater = layoutInflater;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case  R.id.rl_health_manager://健康管理
                GetToast.useString(cnt, "健康管理");
                break;
            case  R.id.ll_house_service://家庭服务
                GetToast.useString(cnt, "家庭服务");
                break;
            case  R.id.rl_pay_money_direc://付费咨询
                GetToast.useString(cnt, "付费咨询");
                break;
        }
    }
    /**
     * 刷新布局
     */
    public void   reFreshMyTvText(DoctorCommnetVo doctorCommnetVo){
        if(doctorCommnetVo == null){
            return ;
        }
        Glide.with(cnt).load(doctorCommnetVo.getHimg()).placeholder(R.drawable.default_head).into(iv_img_discuss);
        tv_discuss_doc_name.setText(TextUtils.isEmpty(doctorCommnetVo.getNick()) ? "" : doctorCommnetVo.getNick());
        tv_is_satisfied.setText(TextUtils.isEmpty(doctorCommnetVo.getNick()) ? "" : doctorCommnetVo.getNick());
      /*  long date2String = DateUtil.getLongToDate(doctorCommnetVo.getPosttime(), "yyyy-MM-dd HH:mm:ss");
        String time =  DateUtil.date2String(date2String, "HH:mm:ss");*/
        String  time = DateUtil.getMyDateToSubYearAndMonthAndDay(doctorCommnetVo.getPosttime());
        tv_discuss_doc_time.setText(TextUtils.isEmpty(time) ? "" : time);
        tv_word_discuss.setText(TextUtils.isEmpty(doctorCommnetVo.getMessage()) ? "暂无评论" : doctorCommnetVo.getMessage());
        String status =  TextUtils.isEmpty(doctorCommnetVo.getScore()) ? "3" : doctorCommnetVo.getScore();
        String is_satisfied1 = "不满意";
        String is_satisfied2 = "一般";
        String is_satisfied3 = "满意";
        if(status.equals("1")){//不满意
            tv_is_satisfied.setText(is_satisfied1);
            ImageUtils.setTextViewBg(tv_is_satisfied, cnt, R.drawable.shade_full_oval_orange, is_satisfied1);
        }else if(status.equals("2")){//一般
            tv_is_satisfied.setText(is_satisfied2);
            ImageUtils.setTextViewBg(tv_is_satisfied, cnt, R.drawable.shade_full_oval_lightblue, is_satisfied2);
        }else if(status.equals("3")){//满意
            tv_is_satisfied.setText(is_satisfied3);
            ImageUtils.setTextViewBg(tv_is_satisfied, cnt, R.drawable.shade_full_oval, is_satisfied3);
        }
        String label = doctorCommnetVo.getLabel();
        mFlowLayout.removeAllViews();
        if(!TextUtils.isEmpty(label)){
            try {
                JSONArray list = new JSONArray(label);
                if(list !=null){
                    for (int i = 0; i <list.length() ; i++) {
                        TextView textView = new TextView(cnt);
                        ImageUtils.setTextViewBg(textView, cnt, R.drawable.shade_enpety_oval, list.getString(i));
                        ViewGroup.MarginLayoutParams marginLayoutParams =  new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 70);
                        marginLayoutParams.setMargins(15, 0, 15, 0);
                        textView.setGravity(Gravity.CENTER);
                        textView.setMinLines(40);

                        if(status.equals("1")){//不满意
                            ImageUtils.setTextViewBg(textView, cnt, R.drawable.shade_enpety_oval_orange, list.getString(i));
                            textView.setTextColor(cnt.getResources().getColor(R.color.color1));
                        }else if(status.equals("2")){//一般
                            ImageUtils.setTextViewBg(textView, cnt, R.drawable.shade_enpety_oval_blue, list.getString(i));
                            textView.setTextColor(cnt.getResources().getColor(R.color.color_blue));
                        }else if(status.equals("3")){//满意
                            ImageUtils.setTextViewBg(textView, cnt, R.drawable.shade_enpety_oval, list.getString(i));
                            textView.setTextColor(cnt.getResources().getColor(R.color.theme_color));
                        }

                        textView.setTextSize(12);
                        textView.setGravity(Gravity.CENTER);
                        mFlowLayout.addView(textView, marginLayoutParams);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

}
